package com.wei.czz.framework.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.czz.common.enums.CommonEnum;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.wei.czz.framework.admin.dao.UserLoginDao;
import com.wei.czz.framework.admin.entity.UserLoginEntity;
import com.wei.czz.framework.admin.service.UserLoginService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2024-06-10 11:07:17
 * className: UserLoginServiceImpl
 * version: 1.0
 * description:
 */
@Service("userLoginService")
@AllArgsConstructor
public class UserLoginServiceImpl extends ServiceImpl<UserLoginDao, UserLoginEntity> implements UserLoginService {

    private static final Logger log = LoggerFactory.getLogger(UserLoginServiceImpl.class);

    @Override
    public void saveOrEdit(UserLoginEntity userLogin) {
        Long id = userLogin.getId();
        if (Objects.isNull(id)) {

            int count = baseMapper.insert(userLogin);
            log.info("查润用户登录记录完成。count={}", count);
        } else {

            int count = baseMapper.updateById(userLogin);
            log.info("修改用户登录记录完成。count={}", count);
        }
    }

    @Override
    public List<UserLoginEntity> findList(Long userId, Integer channel) {
        if (ObjectUtils.allNull(userId, channel)) {
            log.info("获取用户登录记录，传递的用户主键、登录渠道参数为空");
            return Collections.emptyList();
        }
        LambdaQueryWrapper<UserLoginEntity> userLoginLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLoginLambdaQueryWrapper.eq(UserLoginEntity::getUserId, userId)
                .eq(UserLoginEntity::getChannel, channel)
                .eq(UserLoginEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<UserLoginEntity> userLoginList = baseMapper.selectList(userLoginLambdaQueryWrapper);
        log.info("查询用户登录记录完成。size={}", userLoginList.size());
        return userLoginList;
    }

    @Override
    public List<UserLoginEntity> findList(List<Long> userIdList) {
        if (CollectionUtils.isEmpty(userIdList)) {
            log.info("批量获取用户登录记录，传递的用户主键参数为空");
            return Collections.emptyList();
        }
        LambdaQueryWrapper<UserLoginEntity> userLoginLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLoginLambdaQueryWrapper.in(UserLoginEntity::getUserId, userIdList)
                .eq(UserLoginEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<UserLoginEntity> userLoginList = baseMapper.selectList(userLoginLambdaQueryWrapper);
        log.info("查询用户登录记录完成。userIdList.size={} size={}", userIdList.size(), userLoginList.size());
        return userLoginList;
    }

    @Override
    public Long lineCount() {
        long now = System.currentTimeMillis();
        // 统计在线用户数量
        long count = baseMapper.selectLineCount(now, CommonEnum.ZERO.getValue(), CommonEnum.ZERO.getValue());
        log.info("统计在线用户数量完成。count={}", count);
        return count;
    }

    @Transactional
    @Override
    public void batchUpdate(List<UserLoginEntity> userLoginList) {
        boolean bool = this.updateBatchById(userLoginList);
        log.info("批量更新用户登录记录完成。bool={}", bool);
    }
}